package com.example.warehouse.contrallor;


import com.example.warehouse.dto.AssignRoleDto;
import com.example.warehouse.entity.Result;
import com.example.warehouse.entity.Role;
import com.example.warehouse.entity.User;
import com.example.warehouse.page.Page1;
import com.example.warehouse.service.RoleService;
import com.example.warehouse.service.userService;
import com.example.warehouse.utils.CurrentUser;
import com.example.warehouse.utils.DigestUtil;
import com.example.warehouse.utils.TokenUtils;
import com.example.warehouse.utils.WarehouseConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class contrallorUser {
    @Autowired()
    private userService userService;


    @GetMapping("/user-list")
    public Result getpage(Page1 page1,User user){
        Page1 pageQuery = userService.PageQuery(page1, user);
        return Result.ok(pageQuery);
    }

    @PostMapping("/addUser")
    public Result addNewUser(@RequestBody User newuser,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        User user=new User();
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        String md5pwd = DigestUtil.hmacSign(newuser.getUserPwd());
        BeanUtils.copyProperties(newuser,user);
        log.info("新增用户值:{}",user);
        user.setUserPwd(md5pwd);

        user.setUserType(null);
        user.setUserState("1");
        user.setIsDelete("0");
        user.setCreateBy(currentUser.getUserId());
        user.setCreateTime(new Date());
        user.setCreateTime(new Date());
        log.info("新增用户：{}",user);
        userService.addUser(user);
        return Result.ok(user);
    }

    @Autowired
    private TokenUtils tokenUtils;

    @PutMapping("/updateState")
    public Result updateState(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        user.setUpdateBy(currentUser.getUserId());
        boolean boolen = userService.upUserState(user);
        if(!boolen){
            return Result.err(Result.CODE_ERR_BUSINESS,"状态修改失败");
        }
        return Result.ok(Result.CODE_OK);
    }

    @DeleteMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        boolean bl = userService.deleteById(userId);
        if(!bl){
            return Result.err(Result.CODE_ERR_BUSINESS,"用户删除失败");
        }
        return Result.ok("用户信息删除成功");
    }


    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        boolean boolen = userService.updateUserById(user.getUserName(), user.getUserId(),new Date());
        if(!boolen){
            return Result.err(Result.CODE_ERR_BUSINESS,"修改失败");
        }
        return Result.ok(Result.CODE_OK);
    }

    @PutMapping("/updatePwd/{userId}")
    public Result updateUserPwd(@PathVariable Integer userId){

        String OriginPwd = DigestUtil.hmacSign("123456");
        userService.updateUserPwd(userId,OriginPwd);
        return Result.ok(Result.CODE_OK);
    }


    //员工角色
    @Autowired(required = false)
    private RoleService roleService;
    @RequestMapping("/user-role-list/{userId}")
    public Result UserRoleList(@PathVariable Integer userId){
        List<Role> roles = roleService.QueryID(userId);
        return Result.ok(roles);
    }




    @PutMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){
        userService.assignRoleDtoDemo(assignRoleDto);
        return Result.ok("修改成功");
    }

    @RequestMapping("/user-auth")
    public Result userAuth(Integer userId){
        List<Integer> userAuth = userService.findUserAuth(userId);
        return Result.ok(userAuth);
    }


}
